package app_kvServer;

import java.util.ArrayList;
import java.util.List;

public class ServerMetadata {
	private int port;
	private String ipAddress;
	private String rangeStart;
	private String rangeEnd;
	private int version = 0;
	
	
	public ServerMetadata() {}
	
	public ServerMetadata(String ipAddress, int port, String rangeStart, String rangeEnd) {
		this.port = port;
		this.ipAddress = ipAddress;
		this.rangeStart = rangeStart;
		this.rangeEnd = rangeEnd;
		version = 0;
	}
	
	public ServerMetadata(int port, String ipAddress, String rangeStart, String rangeEnd, int version) {
		this(ipAddress, port, rangeStart, rangeEnd);
		this.version = version;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getRangeStart() {
		return rangeStart;
	}

	public void setRangeStart(String rangeStart) {
		this.rangeStart = rangeStart;
	}

	public String getRangeEnd() {
		return rangeEnd;
	}

	public void setRangeEnd(String rangeEnd) {
		this.rangeEnd = rangeEnd;
	}	
	
	public String getVersion() {
		return rangeEnd;
	}

	public void setVersion(int version) {
		this.version = version;
	}	
	
	public static String marshal(List<ServerMetadata> metadata) {
		String ret = "";
		for (ServerMetadata m: metadata) {
			ret += m.getIpAddress()+";"+m.getPort()+";"+m.getRangeStart()+";"+m.getRangeEnd();
			ret += "#";
		} 
		if(ret.length()>=1) {
			ret = ret.substring(0, ret.length()-1);
		}
		return ret;
	}
	
	public static ArrayList<ServerMetadata> unmarshal(String metadata) {
		ArrayList<ServerMetadata> ret = new ArrayList<ServerMetadata>();
		String[] nodes = metadata.split("#");
		for (int i=0; i<nodes.length; i++) {
			String[] params = nodes[i].split(";");
			ret.add(new ServerMetadata(params[0], Integer.parseInt(params[1]), params[2], params[3]));
		}
		return ret;
	}
	
	
}
